package com.yunhe.assist.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.yunhe.assist.domain.ParkIntroduce;
import com.yunhe.assist.domain.ParkIntroduceContact;
import com.yunhe.assist.enums.EnumStat;
import com.yunhe.assist.model.ParkIntroduceSubmitVO;
import com.yunhe.assist.repository.ParkIntroduceRepository;
import com.yunhe.assist.service.ParkIntroduceContactService;
import com.yunhe.assist.service.ParkIntroduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @description park_introduce
 * @author dlz
 * @date 2021-11-09
 */
@Service
public class ParkIntroduceServiceImpl implements ParkIntroduceService {

    @Autowired
    private ParkIntroduceRepository parkIntroduceRepository;
    @Autowired
    private ParkIntroduceContactService parkIntroduceContactService;

    @Override
    public void createParkIntroduce (ParkIntroduce parkIntroduce) {
        parkIntroduce.setCreateTime(new Date());
        parkIntroduce.setUpdateTime(parkIntroduce.getCreateTime());
        parkIntroduce.setStat(EnumStat.NORMAL.getValue());
        parkIntroduceRepository.createParkIntroduce(parkIntroduce);
    }

    @Transactional
    @Override
    public void createParkIntroduce(ParkIntroduceSubmitVO vo) {
        Assert.isTrue(Objects.nonNull(vo.getParkId()), "园区id不能为空");
        Assert.isTrue(StrUtil.isNotBlank(vo.getParkName()), "园区名称不能为空");
        Assert.isTrue(StrUtil.isNotBlank(vo.getContent()), "园区介绍不能为空");

        ParkIntroduce exist = findParkIntroduceByParkId(vo.getParkId());
        if(Objects.nonNull(exist)){
            vo.setId(exist.getId());
            this.updateParkIntroduce(vo);
            return;
        }

        //创建主表信息
        ParkIntroduce parkIntroduce = new ParkIntroduce(){{
           setParkId(vo.getParkId());
           setContent(vo.getContent());
           setParkName(vo.getParkName());
        }};
        this.createParkIntroduce(parkIntroduce);

        //添加联系人
        parkIntroduceContactService.createParkIntroduceContactBatch(vo.getContacts(), parkIntroduce.getId());
    }

    @Override
    public int deleteParkIntroduce(int id) {
        return parkIntroduceRepository.deleteParkIntroduceByPrimaryKey(id);
    }


    @Transactional
    @Override
    public void updateParkIntroduce(ParkIntroduceSubmitVO vo) {
        //更新主表
        ParkIntroduce parkIntroduce = new ParkIntroduce(){{
            setId(vo.getId());
            setContent(vo.getContent());
        }};
        this.updateParkIntroduce(parkIntroduce);

        //更新联系人
        if(Objects.nonNull(vo.getContacts()) && !vo.getContacts().isEmpty()){
            parkIntroduceContactService.deleteByIntroduceId(vo.getId()); //先删除
            parkIntroduceContactService.createParkIntroduceContactBatch(vo.getContacts(), vo.getId()); //再更新
        }
    }

    @Transactional
    @Override
    public void updateParkIntroduce(ParkIntroduce parkIntroduce) {
        Assert.isTrue(Objects.nonNull(parkIntroduce.getId()),"更新失败，主键id异常");
        parkIntroduce.setUpdateTime(new Date());
        parkIntroduceRepository.updateParkIntroduceByPrimaryKeySelective(parkIntroduce);
    }


    @Override
    public ParkIntroduce findParkIntroduceById(int id) {
        return parkIntroduceRepository.searchParkIntroduceByPrimaryKey(id);
    }

    @Override
    public ParkIntroduce findParkIntroduceByParkId(long parkId) {
        return parkIntroduceRepository.findParkIntroduceByParkId(parkId);
    }

    @Override
    public boolean existByParkId(long parkId) {
        return parkIntroduceRepository.existByParkId(parkId) > 0;
    }

    @Override
    public JSONObject findDetailByParkId(long parkId) {
        ParkIntroduce introduce = this.findParkIntroduceByParkId(parkId);
        if(Objects.isNull(introduce)){
            return new JSONObject(){{
                put("introduce", new ParkIntroduce(){{
                    setParkName("");
                    setContent("暂无数据");
                }});
                put("contactList", new ArrayList<>(0));
            }};
        }
        List<ParkIntroduceContact> contactList = parkIntroduceContactService.findList(introduce.getId());
        return new JSONObject(){{
            put("introduce", introduce);
            put("contactList", contactList);
        }};
    }


}